<?php

declare(strict_types=1);

namespace App\Controller\Notify\YunXin;


use App\Controller\BaseController;
use App\Constants\CodeData;
use App\Exception\MessageException;
use App\Services\YunXinCloud\NotifyService;
use App\Services\YunXinCloud\WyyClientService;


class CallbackController extends BaseController
{

    public NotifyService $notifyService;

    /**
     * Note - 云信消息第三方回调
     * DateTime 2024/7/26 17:25
     * User Admin
     * @return false|string
     */
    public function callback()
    {
        $AppKey = $this->request->getHeaderLine('appkey');
        $CurTime = $this->request->getHeaderLine('curtime');
        $MD5 = $this->request->getHeaderLine('md5');
        $CheckSum = $this->request->getHeaderLine('checksum');
        $data = $this->request->all();
        $wyyClient = new WyyClientService();
        if (!$wyyClient->verifyCheckSum($MD5, $CurTime, $CheckSum)) {
            throw new MessageException("签名错误", CodeData::NEED_TIPS);
        }

        return $this->notifyService->callback($data);

    }


    /**
     * Note - 云信消息抄送
     * DateTime 2024/7/26 17:25
     * User Admin
     * @return false|string
     */
    public function copy()
    {
        $data = $this->request->all();
        $CurTime = $this->request->getHeaderLine('curtime');
        $MD5 = $this->request->getHeaderLine('md5');
        $CheckSum = $this->request->getHeaderLine('checksum');
        return $this->notifyService->copy($data, compact('CurTime', 'MD5', 'CheckSum'));
    }

}
